// 在页面或组件中导入
import validate from 'wechat-validate'
Page({
  // 通过 behaviors 注入 validate 方法
  behaviors: [validate],
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime() + 1000 * 60 * 60 * 24 * 3,
    houseList: [],
    houseInfo: '',
    // 提交数据
    houseId: "",
    name: "大勇",
    gender: 0,
    mobile: "18888886666",
    visitDate: ""
  },
  onShow() {
    this.getHouseList()
  },
  // 获取房屋/维修列表
  async getHouseList() {
    const res = await wx.http.get('/room')
    // console.log(houseList);
    const houseList = res.map(({ id, point, building, room }) => ({ id, name: point + building + room }))
    this.setData({ houseList })
  },
  // 选择房屋
  selectHouse(ev) {
    // console.log(ev.detail);
    const { id, name } = ev.detail
    this.setData({ houseId: id, houseInfo: name })
  },
  // 确定访问日期
  onConfirm(ev) {
    // console.log(ev.detail);
    const visitDate = wx.utils.dataFormat(ev.detail)
    this.setData({ visitDate, dateLayerVisible: false })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 确认提交
  async goPassport() {
    if (!this.validate()) return
    const { houseId, name, gender, mobile, visitDate } = this.data
    const { id } = await wx.http.post('/visitor', { houseId, name, gender, mobile, visitDate })
    //  console.log(res);
    wx.utils.toast('添加成功')
    setTimeout(() => {
      wx.reLaunch({
        url: '/visitor_pkg/pages/passport/index?id=' + id,
      })
    }, 1000)
  },
})
